﻿using ECBS_Backend.dao;
using ECBS_Backend.model;
using ECBS_Backend.utils;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ECBS_Backend.service
{
    public class StudentService
    {
        private readonly ECBSContext context;

        private HttpUtils httpUtils;

        public StudentService(ECBSContext context, HttpUtils httpUtils)
        {
            this.context = context;
            this.httpUtils = httpUtils;
        }


        /// <summary>
        /// 学生登陆
        /// </summary>
        /// <param name="studentNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Student StudentLogin(string studentNumber, string password, string schoolName)
        {
            School school = context.Schools.SingleOrDefault(s => s.Name == schoolName);

            var student = context.Students.SingleOrDefault(s => s.StudentNumber == studentNumber && s.SchoolWeId == school.WeID);

            if(student == null){
                throw new Exception("账号未注册！");
            }

            if(student.Password != password){
                throw new Exception("密码错误！");
            }

            return student;
        }

        /// <summary>
        /// 学校增加学生账号
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public Student AddStudent(Student student, string schoolWeId)
        {
            // 验证数据库中是否已经存在该学号
            var stu = context.Students.SingleOrDefault(s => s.StudentNumber == student.StudentNumber);
            if(stu != null){
                throw new Exception("学号重复！");
            }

            student.SchoolWeId = schoolWeId;

            // 获取WeID并赋值给student.WeID
            student.WeID = httpUtils.createWeId();

            // 添加进入数据库
            context.Students.Add(student);

            // 验证是否操作数据库功
            if(context.SaveChanges() == 0){
                throw new Exception("添加失败！");
            }

            return student;
        }

        /// <summary>
        /// 获得学生列表
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="schoolWeId"></param>
        /// <returns></returns>
        public List<string> FindStudent(string keyword, string schoolWeId)
        {
            try
            {
                List<Student> students = context.Students.Where(s => s.SchoolWeId == schoolWeId).ToList();
                if (keyword == null || keyword == "")
                {
                    return students.Select(it => it.StudentNumber).ToList();
                }
                students = students.Where(s => s.StudentNumber.Contains(keyword)).ToList();
                List<string> list = students.Select(it => it.StudentNumber ).ToList();
                return list;
            } catch (Exception e)
            {
                throw new Exception("获取列表出错！");
            }
        }
    }
}
